import React from 'react'
import Component, { mapStateToProps, mapDispatchToProps } from '@components/Component'
import { connect } from 'react-redux'
import PropTypes from 'prop-types'
import { Table, message } from 'antd'
import style from './Index.module.less'

function getColumns () {
  return [
    {
      title: '案号',
      dataIndex: 'caseno_desc'
    },
    {
      title: '案由',
      dataIndex: 'cause_action'
    },
    {
      title: '申请人',
      dataIndex: 'applicant_name'
    },
    {
      title: '被申请人',
      dataIndex: 'respondent_name'
    },
    {
      title: '结案用时',
      dataIndex: 'process_days',
      render (v, l) {
        if (v) {
          return v
        }
        return l.status
      }
    },
    {
      title: '秘书',
      dataIndex: 'processer'
    },
    {
      title: '仲裁员',
      dataIndex: 'arbitratorids',
      render: (v, l) => {
        const list = []
        let ids = []
        let names = []
        if (v) {
          ids = v.split(',')
        }
        if (l.arbitrators) {
          names = l.arbitrators.split(',')
        }
        ids.forEach((id, i) => {
          list.push({ id, name: names[i] })
        })
        return (
          <span className={style.btns}>
            {list.map(li => {
              return <span key={li.id}>{li.name}</span>
            })}
          </span>
        )
      }
    }
  ]
}

class SameCase extends Component {
  state = {
    list: [],
    arbitrator: '',
    arbitratorid: '',
    pagination: {
      current: 1,
      pageSize: 5,
      total: 0,
      showSizeChanger: true,
      showQuickJumper: true,
      showTotal: total => `共 ${total} 件`,
      size: 'small'
    }
  }

  // 获取类案列表
  getList () {
    this.setState({ loading: true })
    const { current, pageSize } = this.state.pagination
    this.$get('/api/xapc/case/caselike', {
      caseid: this.props.caseDetail.id,
      current,
      page_size: pageSize
    }).then(res => {
      this.setState({ loading: false })
      if (res.status === 1) {
        this.setState({
          list: res.data.list.map((li, i) => {
            li.key = pageSize * (current - 1) + i
            return li
          }),
          pagination: {
            ...this.state.pagination,
            total: res.data.total
          }
        })
      } else {
        message.warning(res.message)
      }
    }).catch(() => this.setState({ loading: false }))
  }

  // 初始化选中项
  initSelected () {
    const list = this.state.list
    const { value } = this.props
    for (let i = 0; i < list.length; i++) {
      if (list[i].id === value) {
        this.setState({
          arbitrator: list[i].name,
          arbitratorid: list[i].id
        })
        break
      }
    }
  }

  // 改变选项
  onSelectHandle (v) {
    const o = {
      arbitrator: v.name,
      arbitratorid: v.id
    }
    this.setState(o, () => {
      this.props.onChange(o)
    })
  }

  // 表格请求变化
  tableChange ({ current, pageSize }) {
    this.setState({
      pagination: {
        ...this.state.pagination,
        current,
        pageSize
      }
    }, () => {
      this.getList()
    })
  }

  componentDidUpdate (prev) {
    if (prev.value !== this.props.value) {
      this.initSelected()
    }
  }

  componentDidMount () {
    if (this.props.value) {
      this.initSelected()
    }
    this.getList()
  }

  render () {
    return (
      <Table className={style.mt16} size='small' columns={getColumns.call(this)} dataSource={this.state.list} pagination={this.state.pagination} onChange={v => this.tableChange(v)} />
    )
  }
}

SameCase.propTypes = {
  value: PropTypes.array,
  onChange: PropTypes.func,
  caseDetail: PropTypes.object,
  // eslint-disable-next-line react/no-unused-prop-types
  disabledIdList: PropTypes.array
}
SameCase.defaultProps = {
  value: [],
  onChange: () => {},
  caseDetail: {},
  disabledIdList: [],
  num: 1
}

const mapState = mapStateToProps(function (state) {
  return {
    userInfo: state.basic.userInfo
  }
})

const mapDispatch = mapDispatchToProps(function (dispatch) {
  return {
  }
})

export default connect(mapState, mapDispatch)(SameCase)
